//
//  HomePageScaleCell.swift
//  HappyEat
//
//  Created by ctx2016 on 17/1/9.
//  Copyright © 2017年 陌路是我. All rights reserved.
//

import UIKit

class HomePageScaleCell: UITableViewCell ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    var flowLayout:UICollectionViewFlowLayout?
    //首页收据源
    var dataSources:[HomePageProductModel]?{
        didSet{
//            (count * 128) + ((count - 1) * (k_Wide/8) + 1)
//            flowLayout?.itemSize = CGSize.init(width: k_Wide/2 - 1, height: 128 + 12 + 3*(k_Wide/2)/4)
            //刷新数据
            self.collecTionView?.reloadData()
        }
    }
    
    let CellId:String = "CELLID"
    
    var collecTionView:UICollectionView?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }

}
extension HomePageScaleCell{
    func setUI(){
        self.flowLayout = UICollectionViewFlowLayout.init()
        flowLayout?.itemSize = CGSize.init(width: k_Wide/2 - 1, height: 128 + 12 + 3*(k_Wide/2)/4)
        flowLayout?.minimumLineSpacing = 1
        flowLayout?.minimumInteritemSpacing = 1
        self.collecTionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowLayout!)
        self.contentView.addSubview(self.collecTionView!)
        self.collecTionView?.snp.makeConstraints({ (make) in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
            make.right.equalTo(0)
        })
        self.collecTionView?.backgroundColor = BackGroundColor
        self.collecTionView?.delegate = self
        self.collecTionView?.dataSource = self
        self.collecTionView?.register(HomePageCollectionCell.self, forCellWithReuseIdentifier: CellId)
    }
    @objc(collectionView:didSelectItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("selected")
        //跳转详情界面
        let conTrol:HomePageController = self.getController(view: self) as! HomePageController
        let vc:JieXiaoDetailController = JieXiaoDetailController()
        vc.model = self.dataSources?[indexPath.row]
        conTrol.navigationController?.pushViewController(vc, animated: true)
    }
    @objc(numberOfSectionsInCollectionView:) func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if self.dataSources == nil{
            return 0
        }
        return (self.dataSources?.count)!
    }
    @objc(collectionView:cellForItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let cell = collecTionView?.dequeueReusableCell(withReuseIdentifier: CellId, for: indexPath)as!HomePageCollectionCell
//        cell.nameLabel.text = "芝士蛋糕2016最新款水果味芝士蛋糕"
//        cell.addNameLabel.text = "合肥世纪景园小区"
//        cell.progressView.progress = 0.6
//        cell.needLabel.text = "还需要56人参与"
        cell.model = self.dataSources?[indexPath.row]
        return cell
    }
}
